package cn.ztr.familyalbum.service.impl;

import cn.ztr.familyalbum.dto.GrowthAlbumDocumentsDto;
import cn.ztr.familyalbum.entity.GrowthAlbumDocuments;
import cn.ztr.familyalbum.mapper.GrowthAlbumDocumentsMapper;
import cn.ztr.familyalbum.service.IGrowthAlbumDocumentsService;
import cn.ztr.familyalbum.vo.GrowthAlbumDocumentsVo;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2020-05-28
 */
@Service
public class GrowthAlbumDocumentsServiceImpl extends ServiceImpl<GrowthAlbumDocumentsMapper, GrowthAlbumDocuments> implements IGrowthAlbumDocumentsService {
    @Autowired
    private GrowthAlbumDocumentsMapper growthAlbumDocumentsMapper;

    @Override
    public List selectDocument(GrowthAlbumDocumentsDto growthAlbumDocumentsDto) {
        return growthAlbumDocumentsMapper.selectDocument(growthAlbumDocumentsDto);
    }

    @Override
    public void detailDownload(GrowthAlbumDocumentsDto dto, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(String.valueOf(System.currentTimeMillis()),
                "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        List<GrowthAlbumDocuments> data = growthAlbumDocumentsMapper.selectDocument(dto);
        EasyExcel.write(response.getOutputStream(), GrowthAlbumDocumentsVo.class).sheet("模板").doWrite(data);
    }

    @Override
    public List selectDocumentList() {
        return growthAlbumDocumentsMapper.selectDocumentList();
    }
}
